import { getPayableStatementInfoApi, receivePayableStatementInfoApi } from "@/api/financialManagement/payableManagement";
import { reactive, Ref, ref } from "vue";

export default function InitHook(state: { passData: Ref<StatementPayable.ListRows>; type: Ref<number> }) {
  const { passData, type } = state;
  const formState = reactive<StatementPayable.StatementPayableInfo>({
    id: "",
    paymentBillNumber: "",
    payableStatementNumber: "",
    companyName: "",
    agent: "",
    surplusPayableAmount: 0,
    payableAmount: 0,
    createTime: "",
    createName: "",
    payableAmountSum: 0,
    receivableAmountSum: 0,
    grossProfit: 0,
    statementShipmentListRes: []
  });
  const customerAccountRef = ref<any>(null);
  const loading = ref<boolean>(false);
  const init = () => {
    loading.value = true;
    let requestFn;
    if (type.value === 1) requestFn = getPayableStatementInfoApi;
    else requestFn = receivePayableStatementInfoApi;
    return requestFn(passData.value.id)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          Object.assign(formState, data);
        }
      })
      .finally(() => (loading.value = false));
  };
  const drawerOpts = reactive<Base.DrawerOpts>({
    title: "",
    width: "",
    visible: false,
    component: "",
    type: 0
  });
  const dialogOpts = reactive<Base.DialogOpts>({
    title: "",
    width: "",
    visible: false,
    component: "",
    type: 0
  });
  return {
    formState,
    customerAccountRef,
    init,
    loading,
    drawerOpts,
    dialogOpts
  };
}
